<template>
    <div id="uploadIndex">
        <el-upload
                ref="uploadRef"
                class="upload-demo"
                action="#"
                :auto-upload="true"
                :http-request="requestUpload"
                :limit="1"
                :on-remove="handleRemove"
                :before-upload="beforeUpload">
            <template #trigger>
                <el-icon :size="20">
                    <Plus />
                </el-icon>
            </template>
            <!--显示图片的样式-->
            <div v-if="options.uploadImg">
                <img :src="options.uploadImg" width="100" height="100">
            </div>
            <template #tip>
                <div class="el-upload__tip">
                    只能上传{{limitType}}文件,大小不能超过{{limitSize}}M.
                </div>
            </template>
        </el-upload>
    </div>
</template>

<script>
    import {ElMessage} from "element-plus";
    export default {
        name: "uploadIndex",
        // 接收父组件向子组件的传值
        props:{
            // 文件大小限制
            limitSize:{
                type: Number,
                require: false,
                default: 2
            },
            // 类型限制  默认是图片
            limitType:{
                type: String,
                require: false,
                default: 'image/'
            },
            // 错误信息
            errMsg:{
                type: String,
                require: false,
                default: '文件格式错误，请上传图片类型,如：JPG，PNG后缀的文件。'
            }
        },
        data(){
            return {
                options: {
                    uploadImg: undefined
                }
            }
        },
        methods:{
            // 上传预处理
            beforeUpload(file){
                let isLt2M = file.size / 1024 / 1024 < this.limitSize;
                if (!isLt2M) {
                    ElMessage({
                        showClose: true,
                        message: "上传图片或视频大小不能超过"+this.limitSize+"MB!",
                        type: 'error',
                        duration: 5 * 1000
                    })
                    return false;
                }
                // 验证是否是图片
                if (file.type.indexOf(this.limitType) == -1) {
                    ElMessage({
                        showClose: true,
                        message: this.errMsg,
                        type: 'error',
                        duration: 5 * 1000
                    })
                    return false;
                } else {
                    const reader = new FileReader();
                    reader.readAsDataURL(file);
                    reader.onload = () => {
                        this.options.uploadImg = reader.result;
                    };
                    return true;
                }
            },
            // 图片移除
            handleRemove(param){
                this.options.uploadImg = undefined
            },
            // 覆盖默认的上传行为
            requestUpload(param){
                console.log("子组件上传文件完成回传数据");
                console.log(param.file);
                // 返回给父组件  uploadComplete自定义事件
                this.$emit('uploadComplete',param.file)
            },
        }
    }
</script>

<style scoped>

</style>
